package com.suzhe.ymall.common.pojo;



import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.suzhe.ymall.common.enums.ApiCode;

import java.io.Serializable;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(include =  JsonSerialize.Inclusion.NON_NULL)
//保证序列化json的时候,如果是null的对象,key也会消失
public class ApiResEntity<T> implements Serializable {

    private int status;
    private String msg;
    private T data;
    //如果没有无参的构造器，不能被反序列化
    private ApiResEntity(){

    }

    private ApiResEntity(int status){
        this.status = status;
    }
    private ApiResEntity(int status, T data){
        this.status = status;
        this.data = data;
    }

    private ApiResEntity(int status, String msg){
        this.status = status;
        this.msg = msg;
    }

    @JsonIgnore
    //使之不在json序列化结果当中
    public boolean isSuccess(){
        return this.status == ApiCode.SUCCESS.getCode();
    }

    public int getStatus(){
        return status;
    }
    public T getData(){
        return data;
    }
    public String getMsg(){
        return msg;
    }


    public static <T> ApiResEntity<T> ok(){
        return new ApiResEntity<T>(ApiCode.SUCCESS.getCode());
    }

    public static <T> ApiResEntity<T> ok(T data){
        return new ApiResEntity<T>(ApiCode.SUCCESS.getCode(),data);
    }

    public static <T> ApiResEntity<T> error(){
        return new ApiResEntity<T>(ApiCode.ERROR.getCode(), ApiCode.ERROR.getDesc());
    }


    public static <T> ApiResEntity<T> error(String msg){
        return new ApiResEntity<T>(ApiCode.ERROR.getCode(),msg);
    }

    public static <T> ApiResEntity<T> error(int code, String msg){
        return new ApiResEntity<T>(code,msg);
    }


}
